<?php
require_once OVF_FS_WEB_ROOT."/app/_inc/class.ovfPdf.php";

class ovfDocGen {
	public $aDoc = array();
	public $aAnticipi = array();
	public $nR = -1;
	public $nC = 0;
	public $maxCol = 20;
	public $pdfIntestaz = "";
	public $bottomLeftText = "";
	public $aParRiga = array();
	
	public function __construct() {
		$this->aDoc = array();
		$this->aAnticipi = array();
		$this->nR = -1;
		$this->nC = 0;
		$this->pdfIntestaz = "";
		$this->aParRiga = array();
	}
	
	public function spaziatura($anticipo=0) {
		$this->nR++;
		$this->nC = -1;
		$this->aDoc[$this->nR] = $anticipo;
	}
	
	public function nuovaRiga($anticipo=0) {
		$this->nR++;
		$this->nC = -1;
		$this->aDoc[$this->nR] = array();
		$this->aAnticipi[$this->nR] = $anticipo;
	}
	
	public function setParRiga($par, $val) {
		$this->aParRiga[$this->nR][$par] = $val;
	}
	
	public function setCol($aCol) {
		$this->nC++;
		$this->aDoc[$this->nR][$this->nC] = $aCol;
	}
	
	public function genHtml($noHref=false) {
		$sHtml = "<table class='tab1' border='0' cellpadding='3' cellspacing='1' width='100%' align='center'>";
		$sHtml .= getHtmlRigaRif($this->maxCol);
		foreach($this->aDoc as $rr => $aRX) {
			if (!is_array($aRX)) {
				$sHtml .= "\t<tr>\n";
				$sHtml .= "\t\t<td class='cmd1' colspan='40' style='$style' valign='top'>\n";
				$sHtml .= "\t\t\t&nbsp;\n";
				$sHtml .= "\t\t</td>\n";
				$sHtml .= "\t</tr>\n";
				continue;
			}
			$sHtml .= "\t<tr>\n";
			foreach($aRX as $cc => $aCX) {
				$classe = (isset($aCX['cla'])) ? "class='".$aCX['cla']."'" : "";
				$colspan = (isset($aCX['col']) and intval($aCX['col']) > 1) ?
					"colspan='".intval($aCX['col'])."'" : "";
				$style = "";
				if (isset($aCX['alg'])) {
					if ($aCX['alg']=="C") $style .= "text-align:center;";
					elseif ($aCX['alg']=="R") $style .= "text-align:right;";
				}
				if (isset($aCX['stf'])) {
					if ($aCX['stf']=="B" or $aCX['stf'] == "BI" or $aCX['stf'] == "IB") 
						$style .= "font-weight:bold;";
					if ($aCX['stf']=="I" or $aCX['stf'] == "BI" or $aCX['stf'] == "IB") 
						$style .= "font-style:italic;";
				}
				if (isset($aCX['stl'])) $style .= $aCX['stl'];	
				if (!$noHref && isset($aCX['htm']))	{
					$txt = $aCX['htm'];
				} else {
					$txt =  (isset($aCX['txt'])) ? $aCX['txt'] : "";
					$txt = str_replace("  "," &nbsp;",$txt);
					$txt = str_replace("\n","<br>",$txt);
					if (!$noHref && isset($aCX['hrf'])) {
						$txt = "<a $classe href='".$aCX['hrf']."' >$txt</a>";
					}
				}
				$sHtml .= "\t\t<td $classe $colspan style='$style' valign='top'>\n";
				$sHtml .= "\t\t\t$txt\n";
				$sHtml .= "\t\t</td>\n";
			}
			$sHtml .= "\t</tr>\n";
		}
		$sHtml .= "</table>\n";
		return $sHtml;
	}
	public function genPdf($aOpt=array()) {
		$orientaPagina = (isset($aOpt['orientaPagina']))?$aOpt['orientaPagina']:"P";
		$pageWidth = ($orientaPagina=='L')?257:170;
		$cr1 = 200; $cg1 = 200; $cb1 = 200;
		$pdf=new ovfPdf();
		$pdf->topLeftText = $this->pdfIntestaz;
		//$pdf->topRightImage = EPS_FS_WEB_ROOT.'img/logo_e-stat.jpg';
		$pdf->bottomLeftText = $this->bottomLeftText;
		$pdf->Open();
		$pdf->AddPage($orientaPagina);
		$pdf->SetTextColor(0,0,0);
		$pdf->SetFont('Arial','',7);
		$pdf->SetFillColor(255,255,255);
		foreach($this->aDoc as $rr => $aRX) {
			if (!is_array($aRX)) {
				$pdf->Ln(5);
				continue;
			}
			$aWidths = array();
			$aStyles = array();
			$aAligns = array();
			$aBgColors = array();
			$aCols = array();
			$aBorders = array();
			$kCol = 0;
			foreach($aRX as $cc => $aCX) {	
				$fontSize = 7;			
				$aStyles[$kCol] = "";
				$aAligns[$kCol] = "";
				$aBgColors[$kCol] = null;
				$classe = (isset($aCX['cla'])) ? $aCX['cla'] : "txt1";
				$aBorders[$kCol] = (isset($aCX['brd'])) ? str_pad($aCX['brd'],4,"S") : "SSSS";
				switch ($classe) {
					case 'tit1':
						$aStyles[$kCol] = "B";
						$fontSize = 10;
						$aBgColors[$kCol] = array(200,200,200);
						break;
					case 'lab1':
						$aStyles[$kCol] = "I";
						$aBgColors[$kCol] = array(240,240,240);
						break;
					case 'txt2':
						$aBgColors[$kCol] = array(240,240,240);
						break;
					case 'txt1':
					default :
						break;
				}
				$colspan = (isset($aCX['col']) and intval($aCX['col']) > 1) ? intval($aCX['col']) : 1;
				$aWidths[$kCol] = $colspan*$pageWidth/$this->maxCol;
				$style = "";
				if (isset($aCX['alg'])) {
					if ($aCX['alg']=="C") $aAligns[$kCol] = "C";
					elseif ($aCX['alg']=="R") $aAligns[$kCol] = "R";
				}
				$aStyles[$kCol] = (isset($aCX['stf'])) ? $aCX['stf'] : "";
				$aCols[$kCol] = (isset($aCX['txt'])) ? $aCX['txt'] : "";
				$kCol++;
			}
			if (isset($this->aParRiga[$rr]['pdf_fontSize'])) {
				$fontSize = $this->aParRiga[$rr]['pdf_fontSize'];
			}
			if ($pdf->FontSizePt != $fontSize) {
				$pdf->SetFont('Arial','',$fontSize);
			}
			//Calculate the height of the row
			$hRiga = $pdf->FontSizePt / 2;
			$nb=0;
			for($i=0;$i<count($aCols);$i++) {
				$nb=max($nb,$pdf->NbLines($aWidths[$i],$aCols[$i]));
			}
			$h=$hRiga*($nb+$this->aAnticipi[$rr]);
			//Issue a page break first if needed
			//dbg_("Anticipo=".$this->aAnticipi[$rr].", nb=$nb; hRiga=$hRiga, h=$h, GetY=".$pdf->GetY());
			if($pdf->GetY()+$h>$pdf->PageBreakTrigger) {
				$pdf->AddPage($orientaPagina);
			}		
			$pdf->SetWidths($aWidths);
			$pdf->SetStyles($aStyles);
			$pdf->SetAligns($aAligns);
			$pdf->SetBgColors($aBgColors);
			$pdf->SetBorders($aBorders);
			$pdf->Row($aCols);
		}
		$strPdf = $pdf->Output('doc.pdf','S');
		return $strPdf;
	}
	
} // fine classe epsDocGen